<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/12
 * Time: 3:07
 */

/**
 * 多种请求方法封装
 *
 * @param string $url    请求地址
 * @param string $method 请求方式
 * @param array  $header 请求头
 * @param array  $data   请求体
 *
 * @return bool|string
 */
function Curl_request( $url,$data = [], $method = 'POST',
	$header
	= [
		"Content-type:application/json;charset=utf-8",
		"Accept:application/json",
	]
) {

	$method = strtoupper( $method );
	//初始化
	$ch = curl_init();
	//设置桥接(抓包)
	//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
	//设置请求地址
	curl_setopt( $ch, CURLOPT_URL, $url );
	// 检查ssl证书
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
	// 从检查本地证书检查是否ssl加密
	curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, $url );
	//设置请求方法
	curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );
	//设置请求头
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
	//设置请求数据
	if ( ! empty( $data ) ) {
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
	}
	//设置curl_exec()的返回值以字符串返回
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	$res = curl_exec( $ch );
	curl_close( $ch );

	return $res;
}